package com.team.engine.common.view;

import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.team.engine.basedao.BaseEntity;
import com.team.engine.common.cache.SessionCache;
import com.team.engine.util.SubPage;

public class SubPageInterceptor extends HandlerInterceptorAdapter {

	// 输出页面执行后
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}

	// Controller方法执行后
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		
		Map<HttpSession, Object> jsession = SessionCache.getInstance().getJsession();
		HttpSession session = request.getSession();
		String sessionValue = String.valueOf((Long) jsession.get(session));
		SubPage sub = (SubPage) modelAndView.getModelMap().get("subPage");
		
		// 当执行的Controller方法不是查询，或没使用到分页时，跳过
		if(sub == null)
			return;
		
		// 第一次请求时，还没有分页数据
		boolean b = sessionValue==null || sessionValue.equals("null");
		if(b && sub.getTotalPage() > 0)
			jsession.put(session, sub.getTotalRowNum());
		
//		将页当前数据保存进cookie，在页面点击“上一页”时，使用JS读取cookie里的数据
		List<? extends BaseEntity> resultList = sub.getResultList();
		Cookie[] cookies = request.getCookies();
		if(cookies != null && cookies.length == 1){
			String json = null;
			try {
				json = new ObjectMapper().writeValueAsString(resultList);
			} catch (JsonGenerationException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			Cookie cookie = new Cookie("page" + sub.getThisPage(), json);
			cookie.setMaxAge(-1);
			response.addCookie(cookie);
		}
		
		// 如果查询结果需要再次处理，建议在service层处理掉
		modelAndView.addObject("result", resultList);
		modelAndView.addObject("totalPage", sub.getTotalPage());
		modelAndView.addObject("thisPage", sub.getThisPage());
		
	}

	// Controller方法执行前
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String servletPath = request.getServletPath();
//		LinkedHashSet linkedHashSet = new LinkedHashSet(Arrays.asList(servletPath.split("/")));
//		request.
		
		return true;
	}

}
